package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.ApplyToProgPositionAddDTO;
import org.jsola.hr.dto.ApplyToProgPositionUpdateDTO;
import org.jsola.hr.query.ApplyToProgPositionQuery;
import org.jsola.hr.service.IApplyToProgPositionService;
import org.jsola.hr.vo.ApplyToProgPositionListVO;
import org.jsola.hr.vo.ApplyToProgPositionVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * @author zhr
 */
@RestController("hrApplyToProgPositionController")
@RequestMapping(HrConstants.API_PATH + "/v1/applyToProgPositions")
public class ApplyToProgPositionController implements IApplyToProgPositionControllerAPI {

    @Autowired
    private IApplyToProgPositionService applyToProgPositionService;

    @Override
    @PostMapping
    public Result<ApplyToProgPositionVO> save(@Valid @RequestBody ApplyToProgPositionAddDTO applyToProgPositionAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增方案适用设置岗位权限校验

        return Result.success(
                applyToProgPositionService.save(applyToProgPositionAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody ApplyToProgPositionUpdateDTO applyToProgPositionUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改方案适用设置岗位权限校验

        int count = applyToProgPositionService.update(applyToProgPositionUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{applyToProgPositionId}")
    public Result<Integer> delete(@PathVariable String applyToProgPositionId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除方案适用设置岗位权限校验

        int count = applyToProgPositionService.deleteByIds(tokenUser, applyToProgPositionId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] applyToProgPositionIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除方案适用设置岗位权限校验

        int count = applyToProgPositionService.deleteByIds(tokenUser, applyToProgPositionIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{applyToProgPositionId}")
    public Result<ApplyToProgPositionVO> findById(@PathVariable String applyToProgPositionId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看方案适用设置岗位权限校验

        return Result.success(
            applyToProgPositionService.selectById(applyToProgPositionId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<ApplyToProgPositionListVO>> listAll(@Valid ApplyToProgPositionQuery applyToProgPositionQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看方案适用设置岗位权限校验

        return Result.success(
            applyToProgPositionService.select(applyToProgPositionQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<ApplyToProgPositionListVO>> page(@Valid ApplyToProgPositionQuery applyToProgPositionQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            applyToProgPositionService.selectPage(applyToProgPositionQuery, tokenUser.getSiteId())
        );
    }


}

